﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Saxxon.LibDoomSharp
{
    public class Lump : SerializableBase
    {
        /// <summary>
        /// Raw lump data.
        /// </summary>
        virtual public byte[] Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
            }
        }
        private byte[] _Data;

        /// <summary>
        /// Get or set the length in bytes of the lump. Existing data is preserved where possible.
        /// </summary>
        virtual public int Length
        {
            get
            {
                if (_Data != null)
                    return _Data.Length;
                return 0;
            }
            set
            {
                if (_Data != null)
                {
                    Array.Resize<byte>(ref _Data, value);
                }
                else
                {
                    _Data = new byte[value];
                }
            }
        }

        /// <summary>
        /// Get or set the name of the lump.
        /// </summary>
        virtual public string Name { get; set; }

        /// <summary>
        /// Read lump data.
        /// </summary>
        protected override void Read(Stream stream, int length)
        {
            int remaining = length;
            int offset = 0;
            Data = new byte[length];

            while (remaining > 0)
            {
                int amount = stream.Read(Data, offset, remaining);
                if (amount > 0)
                {
                    offset += amount;
                    remaining -= amount;
                }
            }
        }

        /// <summary>
        /// Read lump data from a file and automatically assign a name based on the file name.
        /// </summary>
        public override void ReadFromFile(string filename)
        {
            Name = Path.GetFileNameWithoutExtension(filename);
            base.ReadFromFile(filename);
        }

        /// <summary>
        /// Interpret lump data as LINEDEFS.
        /// </summary>
        public Lumps.Linedefs ToLinedefs()
        {
            return new Lumps.Linedefs(this);
        }

        /// <summary>
        /// Get the string representation of this Lump.
        /// </summary>
        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// Interpret lump data as THINGS.
        /// </summary>
        public Lumps.Things ToThings()
        {
            return new Lumps.Things(this);
        }

        /// <summary>
        /// Write lump data.
        /// </summary>
        protected override void Write(Stream stream)
        {
            stream.Write(_Data, 0, _Data.Length);
            stream.Flush();
        }
    }
}
